//===============================================================================
// Microsoft patterns & practices
// Web Client Software Factory
//-------------------------------------------------------------------------------
// Copyright (C) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//-------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================
using System;
using System.Configuration;
using System.Globalization;
using System.Reflection;
using WCSFContrib.PageFlow.Configuration;

namespace WCSFContrib.PageFlow
{
	/// <summary>
	/// Directory containing information about PageFlows
	/// </summary>
	//TODO: This class should be split an re-named.
	public static class PageFlowDirectory
	{
		private static IPageFlowProvider _provider;
		private static IPageFlowDefinitionCatalog _catalog;

		private static object _providerLockObject = new object();
		private static object _catalogLockObject = new object();

		/// <summary>
		/// Factory method that returns a singleton instance of a <see cref="IPageFlowProvider"/>
		/// specified in the configuration file's <see cref="PageFlowProviderSection"/>.
		/// </summary>
		public static IPageFlowProvider Provider
		{
			get
			{
				if (_provider == null)
				{
					lock (_providerLockObject)
					{
						if (_provider == null)
						{
							_provider = BuildProvider();
						}
					}
				}
				return _provider;
			}
		}

		/// <summary>
		/// Factory methods that returns a singleton instance of an <see cref="PageFlowDefinitionCatalog"/>.
		/// </summary>
		public static IPageFlowDefinitionCatalog Catalog
		{
			get
			{
				if (_catalog == null)
				{
					lock (_catalogLockObject)
					{
						if (_catalog == null)
						{
							_catalog = new PageFlowDefinitionCatalog();
						}
					}
				}

				return _catalog;
			}
		}

		private static IPageFlowProvider BuildProvider()
		{
			PageFlowProviderSection configSection =
				(PageFlowProviderSection) ConfigurationManager.GetSection("pageFlow/pageFlowProvider");
			PageFlowInstanceStoreProviderSection storeSection =
				(PageFlowInstanceStoreProviderSection) ConfigurationManager.GetSection("pageFlow/pageFlowInstanceStoreProvider");
            PageFlowInstanceCorrelationTokenProviderSection tokenProviderSection =
                (PageFlowInstanceCorrelationTokenProviderSection)ConfigurationManager.GetSection("pageFlow/pageFlowInstanceCorrelationTokenProvider");
            Type providerType = Type.GetType(configSection.ProviderType);
			_provider = (IPageFlowProvider) Activator.CreateInstance(providerType,
			                                                        BindingFlags.CreateInstance,
			                                                        null,
                                                                    new object[] { storeSection, tokenProviderSection },
			                                                        CultureInfo.CurrentCulture);
			return _provider;
		}
	}
}
